package company.session.stateless;

import company.entity.Company;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class CompanySessionBean {

    @PersistenceContext(unitName = "MerlionFoodERP-ejbPU")
    private EntityManager em;

    public void saveNewCompany(Company company) {
        em.persist(company);
    }

    public void saveChanges(Company company) {
        em.merge(company);
    }

    public void deleteCompany(Long id) {
        Company company = em.find(Company.class, id);
        em.remove(company);
    }

    public List<String> getCompanyNames(){
        Query query=em.createQuery("SELECT c.name FROM Company c");
    return query.getResultList();
                }
    public List<Company> getAllCompanies() {
        Query query = em.createQuery("SELECT sl FROM Company sl");
        return query.getResultList();
    }
    
    

    public Company getCompanyById(Long id) {
        return em.find(Company.class, id);
    }
}
